package com.chixing.day14.MapDTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FooTest {
    public static void main(String[] args) {
        Foo f1=  new Foo("测试1","test a") ;
        Foo f2= new Foo("测试2","test b")  ;
        Foo f3= new Foo("测试1","test c");
        Foo f4= new Foo("测试3","test d");
        Foo f5= new Foo("测试3","test e") ;
        Foo f6= new Foo("测试4","test f");
        List<Foo> list =  new ArrayList<>();
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
        list.add(f5);
        list.add(f6);


  /*  //将以上List转换成Map
    Map{
        "测试1", List(Foo("测试1","test a"),Foo("测试1","test c")),
                "测试2", List(Foo("测试2","test b")),
                "测试3", List(Foo("测试3","test d"),Foo("测试3","test e")),
                "测试4", List(Foo("测试4","test f"))
    }*/
    Map<String,List<Foo>> map = new HashMap<>();
        for(Foo fo : list){
            if(map.containsKey(fo.getCode())) {
                List<Foo> fooList = map.get(fo.getCode());
                fooList.add(fo);
            }else{
                List<Foo> foollist = new ArrayList<>();
                foollist.add(fo);
                map.put(fo.getCode(),foollist);
            }
        }
        System.out.println(map);
    }
}
